<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/16
 * Time: 13:54
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Family extends Base
{
    /**
     * 家庭教育首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            //阅读量
            if(isset($params['quantity_start']) && $params['quantity_start'] != '' && isset($params['quantity_end']) && $params['quantity_end'] != ''){
                $where['n_quantity'] = ['between', [$params['quantity_start'], $params['quantity_end']]];
            }
            if(isset($params['quantity_start']) && $params['quantity_start'] != '' && isset($params['quantity_end']) && $params['quantity_end'] == ''){
                $where['n_quantity'] = ['>=', $params['quantity_start']];
            }
            if(isset($params['quantity_start']) && $params['quantity_start'] == '' && isset($params['quantity_end']) && $params['quantity_end'] != ''){
                $where['n_quantity'] = ['<=', $params['quantity_end']];
            }
            
            //日期
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['n_time'] = ['between', [$params['add_start'], $params['add_end']]];
            }
            if(isset($params['add_start']) && $params['add_start'] != '' && isset($params['add_end']) && $params['add_end'] == ''){
                $where['n_time'] = ['>=', $params['add_start']];
            }if(isset($params['add_start']) && $params['add_start'] == '' && isset($params['add_end']) && $params['add_end'] != ''){
                $where['n_time'] = ['<=', $params['add_end']];
            }
            
            //状态
            if(isset($params['n_status']) && $params['n_status'] != ''){
                $where['n_status'] = $params['n_status'];
            }
            
            //通知标题
            if(isset($params['n_title']) && $params['n_title'] != ''){
                $where['n_title'] = ['like', '%' . $params['n_title'] . '%'];
            }
            
            //使用范围
            if(isset($params['n_school']) && $params['n_school'] != ''){
                $n_school = ',' . $params['n_school'] . ',';
                $where['n_school'] = ['like', '%' . $n_school . '%'];
            }
            $where['n_del'] = 1;
            $where['n_type'] = 3;
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->where($where)->count();// 获取总条数
            $lists = Db::name('notice')
                ->field('n_id, n_title, n_thumb, n_time, n_status, n_quantity')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('n_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        return $this->fetch();
    }
    
    /**
     * 添加家庭教育
     * @return mixed|\think\response\Json
     */
    public function familyAdd(){
        if(Request::instance()->isPost()){
            $n_range = input('n_range');
            if($n_range == 1){
                $school_id = Db::table('up_school')
                    ->where('school_status', 1)
                    ->where('school_del', 1)
                    ->column('school_id');
                $n_school = ',' . implode(',', $school_id) . ',';
            }else{
                if(input('n_school') == ''){
                    return json(['code' => 100, 'msg' => '请选择学校']);
                }
                $n_school = ',' . input('n_school') . ',';
            }
            $n_title = trim(input('n_title'), ' ');
            $n_title_num = mb_strlen($n_title, 'utf-8');
            if($n_title_num > 60){
                return json(['code' => 100, 'msg' => '标题不可超过60字']);
            }
            $data = [
                'n_type' => 3,
                'n_title' => $n_title,
                'n_thumb' => input('n_thumb'),
                'n_content' => input('n_content'),
                'n_school' => $n_school,
                'n_time' => date('Y-m-d H:i:s'),
                'n_range' => $n_range
            ];
            $res = Db::table('up_notice')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $school = Db::table('up_school')
            ->where('school_status', 1)
            ->where('school_del', 1)
            ->field('school_id, school_name')
            ->select();
        $this->assign([
            'school' => $school
        ]);
        return $this->fetch('family/familyadd');
    }
    
    /**
     * 编辑家庭教育
     * @return mixed|\think\response\Json
     */
    public function familyEdit(){
        if(Request::instance()->isPost()){
            $id = input('n_id');
            $n_title = trim(input('n_title'), ' ');
            $n_title_num = mb_strlen($n_title, 'utf-8');
            if($n_title_num > 60){
                return json(['code' => 100, 'msg' => '标题不可超过60字']);
            }
            $data = [
                'n_title' => $n_title,
                'n_content' => input('n_content'),
            ];
            if(!empty(input('n_thumb'))){
                $data['n_thumb'] = input('n_thumb');
            }
            $res = Db::table('up_notice')->where('n_id', $id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $n_id = input('n_id');
        $info = Db::table('up_notice')
            ->field('n_id, n_title, n_thumb, n_content')
            ->where('n_id', $n_id)
            ->find();
        
        $this->assign([
            'n_id' => $n_id,
            'info' => $info
        ]);
        return $this->fetch('family/familyedit');
    }
    
    /**
     * 家庭教育详情
     * @return mixed
     */
    public function familyDetail(){
        $n_id = input('n_id');
        $info = Db::table('up_notice')
            ->field('n_id, n_title, n_thumb, n_content, n_school, n_quantity')
            ->where('n_id', $n_id)
            ->find();
        $school_id = trim($info['n_school'], ',');
        $school_name = Db::table('up_school')->where('school_id', 'IN', $school_id)->column('school_name');
        $info['school_name'] = implode(',', $school_name);
        $this->assign([
            'n_id' => $n_id,
            'info' => $info
        ]);
        return $this->fetch('family/familydetail');
    }
    
    /**
     * 批量删除家庭教育
     * @return \think\response\Json
     */
    public function familyDel(){
        $ids = input('ids');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择要删除的家庭教育']);
        }
        $result = Db::table('up_notice')->where('n_id', 'IN', $ids)->setField('n_del', 2);
        if($result){
            return json(['code' => '200', 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function familyStatus(){
        $ids = input('ids');
        $num = input('num');
        if($ids == ''){
            return json(['code' => 100, 'msg' => '请选择需要修改的家庭教育']);
        }
        $status = Db::table('up_notice')->where('n_id', 'IN', $ids)->column('n_status');
        if($num == 1){//上架
            if(in_array(1, $status)){
                return json(['code' => 100, 'msg' => '所选家庭及教育中含有上架中记录，请重新选择']);
            }
            $msg = '上架成功';
        }else{//下架
            if(in_array(2, $status)){
                return json(['code' => 100, 'msg' => '所选家庭教育中含有已下架记录，请重新选择']);
            }
            $msg = '下架成功';
        }
        $res = Db::table('up_notice')->where('n_id', 'IN', $ids)->setField('n_status', $num);
        if($res){
            return json(['code' => 200, 'msg' => $msg]);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}